

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Cheney">
  <meta name="keywords" content="Coding">
  
    <meta name="description" content="该算法将系统中的进程就绪队列从一个拆分为若干个，将不同类型或性质的进程固定分配在不同的就绪队列，不同的就绪队列采用不同的调度算法，一个就绪队列中的进程可以设置不同的优先级，不同的就绪队列本身也可以设置不同的优先级。多级队列调度算法由于设置多个就绪队列，因此对每个就绪队列就可以实施不同的调度算法，因此，系统针对不同用户进程的需求，很容易提供多种调度策略。">
<meta property="og:type" content="article">
<meta property="og:title" content="多级队列调度和多级反馈队列调度算法的实现">
<meta property="og:url" content="https://cheney822.gitee.io/2021/05/04/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F__%E5%A4%9A%E7%BA%A7%E9%98%9F%E5%88%97%E8%B0%83%E5%BA%A6%E5%92%8C%E5%A4%9A%E7%BA%A7%E5%8F%8D%E9%A6%88%E9%98%9F%E5%88%97%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95%E7%9A%84%E5%AE%9E%E7%8E%B0/index.html">
<meta property="og:site_name" content="Cheney blog">
<meta property="og:description" content="该算法将系统中的进程就绪队列从一个拆分为若干个，将不同类型或性质的进程固定分配在不同的就绪队列，不同的就绪队列采用不同的调度算法，一个就绪队列中的进程可以设置不同的优先级，不同的就绪队列本身也可以设置不同的优先级。多级队列调度算法由于设置多个就绪队列，因此对每个就绪队列就可以实施不同的调度算法，因此，系统针对不同用户进程的需求，很容易提供多种调度策略。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210411194558752.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/992a49478a0a42869b872971a13fc151.gif">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/20210408223946522-20220405205748739.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/20210408224023836-20220405205748773.png">
<meta property="article:published_time" content="2021-05-04T00:22:00.000Z">
<meta property="article:modified_time" content="2022-04-05T13:14:30.435Z">
<meta property="article:author" content="Cheney">
<meta property="article:tag" content="算法">
<meta property="article:tag" content="C语言">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20210411194558752.png">
  
  
  <title>多级队列调度和多级反馈队列调度算法的实现 - Cheney blog</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"cheney822.gitee.io","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":1},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Cheney Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://imgbed.cheney.cc/Blog_config/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="多级队列调度和多级反馈队列调度算法的实现">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-05-04 08:22" pubdate>
        2021年5月4日 早上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      5.6k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      28 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">多级队列调度和多级反馈队列调度算法的实现</h1>
            
            <div class="markdown-body">
              <h3 id="多级队列调度算法"><a href="#多级队列调度算法" class="headerlink" title="多级队列调度算法"></a>多级队列调度算法</h3><ul>
<li>多级队列：该算法将系统中的进程就绪队列从一个拆分为若干个，将不同类型或性质的进程固定分配在不同的就绪队列，不同的就绪队列采用不同的调度算法，一个就绪队列中的进程可以设置不同的优先级，不同的就绪队列本身也可以设置不同的优先级。<br>多级队列调度算法由于设置多个就绪队列，因此对每个就绪队列就可以实施不同的调度算法，因此，系统针对不同用户进程的需求，很容易提供多种调度策略。</li>
</ul>
<h3 id="题目描述："><a href="#题目描述：" class="headerlink" title="题目描述："></a>题目描述：</h3><ul>
<li>设RQ分为RQ1和RQ2</li>
<li>RQ1采用轮转法，时间片q=7.</li>
<li>RQ1&gt;RQ2</li>
<li>RQ2采用短进程优先调度算法。</li>
<li>测试数据如下：</li>
<li>其中：RQ1: P1-P5,   RQ2: P6-P10　</li>
</ul>
<table>
<thead>
<tr>
<th>进程</th>
<th>P1</th>
<th>P2</th>
<th>P3</th>
<th>P4</th>
<th>P5</th>
<th>P6</th>
<th>P7</th>
<th>P8</th>
<th>P9</th>
<th>P10</th>
</tr>
</thead>
<tbody><tr>
<td>运行时间</td>
<td>16</td>
<td>11</td>
<td>14</td>
<td>13</td>
<td>15</td>
<td>21</td>
<td>18</td>
<td>10</td>
<td>7</td>
<td>14</td>
</tr>
<tr>
<td>已等待时间</td>
<td>6</td>
<td>5</td>
<td>4</td>
<td>3</td>
<td>2</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</tbody></table>
<h3 id="程序功能"><a href="#程序功能" class="headerlink" title="程序功能"></a>程序功能</h3><ul>
<li>对于给定的数据使用多级队列调度算法进行分析计算周转时间。其中多级队列分为RQ1和RQ2 ，RQ1采用的是时间片长度为7的时间片轮转算法，RQ2采用的是短进程优先算法。并且RQ1的优先级高于RQ2（即只有在RQ1内所有程序运行结束，RQ2才能开始运行）</li>
</ul>
<h3 id="设计思路"><a href="#设计思路" class="headerlink" title="设计思路"></a>设计思路</h3><ul>
<li>时间片轮转：首先对RQ1按照等待时间长短排序，然后从头设置循环，只要队列不空就一直进行下去，每次取队头RQ1的下一个元素（RQ1仅用作标志，不存储数据）判断need是否小于等于时间片大小，小于等于则置为0后踢出队列进入finish队列，大于则将need减去时间片大小，然后将其移动至队尾。</li>
<li>短进程优先：开始前需要对RQ2按照剩余执行时间大小进行排序，与时间片轮转法类似，不同的是这里一旦开始执行就直接执行完毕，然后下一个进程上处理机运行。</li>
</ul>
<center> <img src="https://img-blog.csdnimg.cn/20210411194558752.png" srcset="/img/loading.gif" lazyload width="60%">



<h3 id="数据结构"><a href="#数据结构" class="headerlink" title="数据结构"></a>数据结构</h3><ul>
<li>本程序每个进程用一个PCB表示，每个PCB内含有name（标识符）、need（当前仍然需要多长时间才能运行结束）、turn（周转时间（等于等待时间+运行时间））、next指针（指向等待队列的下一个进程）。两个队列的头节点分别为RQ1、RQ2还有一个结束队列Finish（运行结束后进程从原队列进入这里）</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span> <span class="hljs-title class_">tag_pcb</span> &#123;<br>    <span class="hljs-type">char</span> name[<span class="hljs-number">8</span>];<br>    <span class="hljs-type">int</span> need = <span class="hljs-number">0</span>;<span class="hljs-comment">//需要运行的时间</span><br>    <span class="hljs-type">int</span> turn = <span class="hljs-number">0</span>;<span class="hljs-comment">//周转时间=等待时间+运行时间</span><br>    <span class="hljs-keyword">struct</span> <span class="hljs-title class_">tag_pcb</span>* next = <span class="hljs-literal">NULL</span>;<br>&#125;PCB;<br>PCB* RQ1=<span class="hljs-keyword">new</span> PCB, * RQ2 = <span class="hljs-keyword">new</span> PCB, * Finish = <span class="hljs-keyword">new</span> PCB;<br></code></pre></td></tr></table></figure>

<h3 id="代码实现："><a href="#代码实现：" class="headerlink" title="代码实现："></a>代码实现：</h3><center> <img src="https://img-blog.csdnimg.cn/992a49478a0a42869b872971a13fc151.gif" srcset="/img/loading.gif" lazyload width="60%">



<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;iostream&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;fstream&gt;</span></span><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> std;<br><br><br><span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span> <span class="hljs-title class_">tag_pcb</span> &#123;<br>    <span class="hljs-type">char</span> name[<span class="hljs-number">8</span>];<br>    <span class="hljs-type">int</span> need = <span class="hljs-number">0</span>;<span class="hljs-comment">//需要运行的时间</span><br>    <span class="hljs-type">int</span> turn = <span class="hljs-number">0</span>;<span class="hljs-comment">//周转时间=等待时间+运行时间</span><br>    <span class="hljs-keyword">struct</span> <span class="hljs-title class_">tag_pcb</span>* next = <span class="hljs-literal">NULL</span>;<br>&#125;PCB;<br>PCB* RQ1=<span class="hljs-keyword">new</span> PCB, * RQ2 = <span class="hljs-keyword">new</span> PCB, * Finish = <span class="hljs-keyword">new</span> PCB;<br><span class="hljs-type">const</span> <span class="hljs-type">int</span> TimePiece = <span class="hljs-number">7</span>;<span class="hljs-comment">//时间片长度</span><br><br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">ReadFile</span><span class="hljs-params">()</span></span>&#123;<br><br>    <span class="hljs-function">ifstream <span class="hljs-title">In</span><span class="hljs-params">(<span class="hljs-string">&quot;RQ1.txt&quot;</span>)</span></span>;<br>    PCB* Current = RQ1;<br><br>    <span class="hljs-keyword">while</span> (!In.<span class="hljs-built_in">eof</span>()) &#123;<br>        PCB* Cur = <span class="hljs-keyword">new</span> PCB;<br><br>        In &gt;&gt; Cur-&gt;name &gt;&gt; Cur-&gt;need&gt;&gt; Cur-&gt;turn;<br><br>        Current-&gt;next = Cur;<br>        Current = Current-&gt;next;<br>    &#125;<br>    In.<span class="hljs-built_in">close</span>();<br><br>    <span class="hljs-function">ifstream <span class="hljs-title">In1</span><span class="hljs-params">(<span class="hljs-string">&quot;RQ2.txt&quot;</span>)</span></span>;<br>    PCB* Current1 = RQ2;<br><br>    <span class="hljs-keyword">while</span> (!In1.<span class="hljs-built_in">eof</span>()) &#123;<br>        PCB* Cur1 = <span class="hljs-keyword">new</span> PCB;<br><br>        In1 &gt;&gt; Cur1-&gt;name &gt;&gt; Cur1-&gt;need &gt;&gt; Cur1-&gt;turn;<br>        Current1-&gt;next = Cur1;<br>        Current1 = Current1-&gt;next;<br>    &#125;<br>    In1.<span class="hljs-built_in">close</span>();<br>&#125;<br><br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Q1_Insert</span><span class="hljs-params">(PCB a)</span> </span>&#123; <span class="hljs-comment">//时间片轮转算法队列的插入(插入尾部)</span><br><br>    PCB* Current = RQ1;<br>    <span class="hljs-keyword">while</span> (Current-&gt;next != <span class="hljs-literal">NULL</span>)<br>        Current = Current-&gt;next;<br>    Current-&gt;next = <span class="hljs-keyword">new</span> PCB;<br><br>    *Current-&gt;next = a;<br>    <span class="hljs-comment">//Current-&gt;next = &amp;a;</span><br><br>    Current-&gt;next-&gt;next = <span class="hljs-literal">NULL</span>;<br><br>&#125;<br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Q2_Insert</span><span class="hljs-params">(PCB b)</span> </span>&#123; <span class="hljs-comment">//短进程优先调度算法队列的插入</span><br><br>    <br>    PCB* Current = RQ2;<br>    <span class="hljs-keyword">while</span> (Current-&gt;next != <span class="hljs-literal">NULL</span>)<br>        Current = Current-&gt;next;<br>    Current-&gt;next = <span class="hljs-keyword">new</span> PCB;<br><br>    *Current-&gt;next = b;<br>    Current-&gt;next-&gt;next = <span class="hljs-literal">NULL</span>;<br>    <br>&#125;<br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Fin_Insert</span><span class="hljs-params">(PCB c)</span> </span>&#123; <span class="hljs-comment">//短进程优先调度算法队列的插入</span><br>    <br>    PCB* cc = <span class="hljs-keyword">new</span> PCB;<br>    *cc = c;<br><br>    cc-&gt;next = Finish-&gt;next;<br>    Finish-&gt;next = cc;   <br>&#125;<br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Q2_sort</span><span class="hljs-params">(PCB *T)</span> </span>&#123;<br><br>    PCB* X = <span class="hljs-keyword">new</span> PCB;<span class="hljs-comment">//用来保存排序后的链表</span><br>    PCB* p = <span class="hljs-keyword">new</span> PCB;<span class="hljs-comment">//用来保存当此最小值的前一位</span><br>    PCB* Current = T-&gt;next;<br>    PCB * PreCurrent = T;<br>    PCB* TailX = X;<br>    <br>    <span class="hljs-keyword">while</span> (T-&gt;next != <span class="hljs-literal">NULL</span>) &#123;<br>        <span class="hljs-type">int</span> tem = <span class="hljs-number">999999</span>;<br><br>        Current = T-&gt;next;<br>        PreCurrent = T;<br>        <span class="hljs-keyword">while</span> (Current != <span class="hljs-literal">NULL</span>) &#123;<br>            <span class="hljs-keyword">if</span> (Current-&gt;need &lt; tem) &#123;<br>                tem = Current-&gt;need;<br>                <br>                p = PreCurrent;<br>                <span class="hljs-comment">//cout &lt;&lt; &quot;处理&quot; &lt;&lt; p-&gt;name &lt;&lt; p-&gt;need &lt;&lt; &quot;\n&quot;;</span><br>            &#125;<br>            Current = Current-&gt;next;<br>            PreCurrent = PreCurrent-&gt;next;<br>        &#125;<br>     <br><br>        TailX-&gt;next = p-&gt;next;<br>        TailX = TailX-&gt;next;<br><br>        <span class="hljs-keyword">if</span> (p-&gt;next-&gt;next != <span class="hljs-literal">NULL</span>)<br>            p-&gt;next = p-&gt;next-&gt;next;<br>        <span class="hljs-keyword">else</span><br>            p-&gt;next = <span class="hljs-literal">NULL</span>;      <br>    &#125;<br>    *T = *X;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-built_in">ReadFile</span>();<br><br>    <span class="hljs-type">int</span> clock = <span class="hljs-number">0</span>; <span class="hljs-comment">//时钟</span><br>    <span class="hljs-keyword">while</span> (RQ1-&gt;next != <span class="hljs-literal">NULL</span>) &#123;<span class="hljs-comment">//表示RQ1还有元素</span><br>        <span class="hljs-type">int</span> t = TimePiece;<br>        PCB* Current = RQ1-&gt;next;<br>        <span class="hljs-type">int</span> fin = <span class="hljs-number">0</span>;<br><br>        <span class="hljs-keyword">if</span> (Current-&gt;need &lt;= t)<br>            t = Current-&gt;need, fin = <span class="hljs-number">1</span>;<br>           <br>        clock += t;<span class="hljs-comment">//表示pi运行t</span><br><br>        <span class="hljs-comment">//输出计算过程</span><br>        <span class="hljs-comment">//cout &lt;&lt; &quot;\n&quot; &lt;&lt; Current-&gt;name &lt;&lt; &quot;_____&quot; &lt;&lt; Current-&gt;turn &lt;&lt; &quot;__+ ___&quot; &lt;&lt; clock &lt;&lt; &quot;__= ___&quot; &lt;&lt; Current-&gt;turn +clock &lt;&lt; &quot;\n&quot;;</span><br>       <br>        Current-&gt;need -= t;<br>  <br>        <span class="hljs-keyword">if</span> (fin)<br>            Current-&gt;turn += clock, <span class="hljs-built_in">Fin_Insert</span>(*Current);<span class="hljs-comment">//运行结束    </span><br>        <span class="hljs-keyword">else</span><br>            <span class="hljs-built_in">Q1_Insert</span>(*Current);<span class="hljs-comment">//进入队尾等待运行</span><br><br>        <span class="hljs-keyword">if</span> (Current-&gt;next == <span class="hljs-literal">NULL</span>)<br>            <span class="hljs-keyword">break</span>;<br>        RQ1-&gt;next = Current-&gt;next;<br>    &#125;<br><br><br>    clock = <span class="hljs-number">0</span>;<span class="hljs-comment">//时钟要清空一次</span><br>    <span class="hljs-built_in">Q2_sort</span>(RQ2);<span class="hljs-comment">//先排序</span><br><br>    cout &lt;&lt; <span class="hljs-string">&quot;RQ2:__&quot;</span>; <br>    <span class="hljs-keyword">for</span> (PCB* Current2 = RQ2-&gt;next; Current2 != <span class="hljs-literal">NULL</span>; Current2 = Current2-&gt;next)<br>        cout &lt;&lt; Current2-&gt;name &lt;&lt; <span class="hljs-string">&quot;--&quot;</span>;<br><br>    <span class="hljs-keyword">while</span> (RQ2-&gt;next != <span class="hljs-literal">NULL</span>) &#123;<span class="hljs-comment">//表示RQ2还有元素(到这一步默认RQ1已经为空)</span><br>        PCB* Current3 = RQ2-&gt;next;<br>        <span class="hljs-type">int</span> t = Current3-&gt;need;<br><br>        clock += t;<span class="hljs-comment">//表示pi运行t</span><br>        Current3-&gt;need -= t;<span class="hljs-comment">//实质为清空</span><br>        Current3-&gt;turn += clock;<br><br>        <span class="hljs-built_in">Fin_Insert</span>(*Current3);<br><br>        <span class="hljs-keyword">if</span> (Current3-&gt;next == <span class="hljs-literal">NULL</span>)<br>            <span class="hljs-keyword">break</span>;<br>        RQ2-&gt;next = Current3-&gt;next;<br>    &#125;<br><br>    <span class="hljs-type">int</span> SUM = <span class="hljs-number">0</span>;<br>    <span class="hljs-keyword">for</span> (PCB* Current2 = Finish-&gt;next; Current2 != <span class="hljs-literal">NULL</span>; Current2 = Current2-&gt;next) &#123;<br>        cout &lt;&lt; <span class="hljs-string">&quot;\n&quot;</span> &lt;&lt; Current2-&gt;name &lt;&lt;<span class="hljs-string">&quot;\t&quot;</span>&lt;&lt; Current2-&gt;turn ;<br>        SUM += Current2-&gt;turn;<br>    &#125;<br><br>    cout &lt;&lt; <span class="hljs-string">&quot;\n总周转时间为：&quot;</span> &lt;&lt; SUM &lt;&lt; <span class="hljs-string">&quot;\n&quot;</span>;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<ul>
<li>多级队列调度测试结果：<br><img src="https://imgbed.cheney.cc/picgo/20210408223946522-20220405205748739.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></li>
</ul>
<h3 id="附："><a href="#附：" class="headerlink" title="附："></a>附：</h3><p><strong>多级反馈队列调度算法如下原理：</strong></p>
<ul>
<li>1、设有N个队列（Q1,Q2….QN），其中各个队列对于处理机的优先级是不一样的，也就是说位于各个队列中的作业(进程)的优先级也是不一样的。一般来说，优先级Priority(Q1) &gt; Priority(Q2) &gt; … &gt; Priority(QN)。怎么讲，位于Q1中的任何一个作业(进程)都要比Q2中的任何一个作业(进程)相对于CPU的优先级要高（也就是说，Q1中的作业一定要比Q2中的作业先被处理机调度），依次类推其它的队列。</li>
<li>2、对于优先级最低的队列来说，里面是遵循时间片轮转法。也就是说，位于队列QN中有M个作业，它们的运行时间是通过QN这个队列所设定的时间片来确定的；对于其他队列，遵循的是先来先服务算法，每一进程分配一定的时间片，若时间片运行完时进程未结束，则进入下一优先级队列的末尾。</li>
<li>3、各个队列的时间片是一样的吗？<br>不一样，这就是该算法设计的精妙之处。各个队列的时间片是随着优先级的增加而减少的，也就是说，优先级越高的队列中它的时间片就越短。同时，为了便于那些超大作业的完成，最后一个队列QN(优先级最低的队列)的时间片一般很大(不需要考虑这个问题)。</li>
</ul>
<p>上述程序在某一进程在一级队列运行一轮后没有运行完毕，若加入二级队列而不是加入原队列的尾部，则可以实现简单的多级反馈队列调度算法<br>两种算法的<strong>不同之处</strong>就在于：当一个RQ1中的进程在时间片结束之后是回到当前的队尾还是到RQ2队列之中。<br>在上述程序中也很容易实现：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">if</span> (fin)<br>          Current-&gt;turn += clock, <span class="hljs-built_in">Fin_Insert</span>(*Current);<span class="hljs-comment">//运行结束    </span><br>      <span class="hljs-keyword">else</span><br>          <span class="hljs-built_in">Q1_Insert</span>(*Current);<span class="hljs-comment">//进入队尾等待运行</span><br></code></pre></td></tr></table></figure>

<p>修改为：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">if</span> (fin)<br>          <span class="hljs-built_in">Fin_Insert</span>(*Current);<span class="hljs-comment">//运行结束    </span><br>      <span class="hljs-keyword">else</span><br>          <span class="hljs-built_in">Q2_Insert</span>(*Current);<span class="hljs-comment">//进入二级队列等待运行</span><br>      Current-&gt;turn += clock, <br></code></pre></td></tr></table></figure>

<p>上述两种代码分别实现了上述两种功能，执行时只需选一种在相应位置即可。</p>
<ul>
<li>多级反馈队列调度测试结果：<img src="https://imgbed.cheney.cc/picgo/20210408224023836-20220405205748773.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></li>
</ul>
<p>由分析上述数据容易发现：在该测试数据的情况下多级反馈队列调度算法是要优于多级队列调度的</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">操作系统</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E7%AE%97%E6%B3%95/">算法</a>
                    
                      <a class="hover-with-bg" href="/tags/C%E8%AF%AD%E8%A8%80/">C语言</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/05/05/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F__%E8%AF%B7%E6%B1%82%E5%88%86%E9%A1%B5%E7%B3%BB%E7%BB%9F%E4%B8%AD%E7%9A%84%E7%BD%AE%E6%8D%A2%E7%AE%97%E6%B3%95(FIFO%E3%80%81LRU%E3%80%81Optimal)/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">请求分页系统中的置换算法(FIFO、LRU、Optimal)</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/05/03/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F__%E7%A3%81%E7%9B%98%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95%EF%BC%88%E5%85%88%E6%9D%A5%E5%85%88%E6%9C%8D%E5%8A%A1%E3%80%81%E6%9C%80%E7%9F%AD%E5%AF%BB%E9%81%93%E4%BC%98%E5%85%88%E4%BB%A5%E5%8F%8A%E7%94%B5%E6%A2%AF%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95%EF%BC%89/">
                        <span class="hidden-mobile">磁盘调度算法（先来先服务、最短寻道优先以及电梯调度算法）</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://cheney822.gitee.io/" target="_blank" rel="nofollow noopener"><span>备用网址</span></a> 
  </div>
  

  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        皖ICP备2022002876号-1
      </a>
    </span>
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
